﻿<UserControl x:Class="EtlViewer.Viewer.Controls.EventsGrid"
             x:ClassModifier="internal"
             Name="usercontrol"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:l="clr-namespace:EtlViewer.Viewer.Controls" 
             xmlns:utils="clr-namespace:EtlViewer.Viewer.UIUtils" 
             xmlns:ctrls="clr-namespace:EtlViewer.Viewer.Controls" 
             mc:Ignorable="d"              
             d:DesignHeight="600" d:DesignWidth="1200">
    <UserControl.Resources>
        <utils:GuidToStringConverter x:Key="GuidToStringConverter" />
        <utils:CopyWithHeadersCommand x:Key="CopyWithHeadersCommand" />
        <Style x:Key="ColumnsFilterListBoxStyle">
            <Style.Resources>
                <!-- Background of selected item when focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF1974aa" />
                <!-- Background of selected item when not focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF1974aa" />
            </Style.Resources>
        </Style>
        <utils:SearchTermConverter x:Key="SearchTermConverter"/>
        <Style x:Key="RightAlign" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource GridCellStyle}">
            <Setter Property="TextBlock.TextAlignment" Value="Right" />
        </Style>

        <Style x:Key="SeachCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource GridCellStyle}">
            <EventSetter Event="PreviewKeyDown" Handler="gridEvents_PreviewKeyDown"/>
            <Setter Property="utils:SearchOperations.IsMatch">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource SearchTermConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="(utils:SearchOperations.SearchTerm)" />
                        <Binding Path="ActivityId" Converter="{StaticResource GuidToStringConverter}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="utils:SearchOperations.IsMatch" Value="True">                    
                    <Setter Property="Background" Value="{DynamicResource SearchHighlightBackground}" />
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition  Property="utils:SearchOperations.IsMatch" Value="True"></Condition>
                        <Condition Property="IsSelected" Value="True"></Condition>
                    </MultiTrigger.Conditions>
                    <Setter Property="Foreground" Value="White"></Setter>
                </MultiTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="RelatedCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource SeachCell}">
            <Setter Property="utils:SearchOperations.IsMatch">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource SearchTermConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="(utils:SearchOperations.SearchTerm)" />
                        <Binding Path="RelatedActivityId" Converter="{StaticResource GuidToStringConverter}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ActivityIdCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource SeachCell}">
            <Setter Property="utils:SearchOperations.IsMatch">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource SearchTermConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="(utils:SearchOperations.SearchTerm)" />
                        <Binding Path="ActivityId" Converter="{StaticResource GuidToStringConverter}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="MessageDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource SeachCell}">
            <Setter Property="TextBlock.TextTrimming" Value="WordEllipsis" />
            <Setter Property="utils:SearchOperations.IsMatch">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource SearchTermConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="(utils:SearchOperations.SearchTerm)" />
                        <Binding  Path="Message"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="IdDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource SeachCell}">
            <Setter Property="TextBlock.TextAlignment" Value="Right" />
            <Setter Property="utils:SearchOperations.IsMatch">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource SearchTermConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="(utils:SearchOperations.SearchTerm)" />
                        <Binding  Path="Id"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SymbolDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource SeachCell}">
            <Setter Property="utils:SearchOperations.IsMatch">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource SearchTermConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="(utils:SearchOperations.SearchTerm)" />
                        <Binding  Path="Symbol"/>
                        <Binding Path="ActivityId" Converter="{StaticResource GuidToStringConverter}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Popup Name="ColumnChooser" 
               PlacementTarget ="{Binding ElementName=gridEvents}" 
               Placement="Mouse"
               StaysOpen="False">
            <Grid>
                <Border Margin="0"  
                        Padding="5"
                        BorderBrush="Black"
                        Background="White"
                        BorderThickness="1">
                    <Grid Width="250">
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0">Choose Columns</TextBlock>
                        <ListBox Grid.Row="1"  Width="300" 
                                 x:Name="ColumnsFilter" 
                                 VerticalAlignment="Top" 
                                 IsSynchronizedWithCurrentItem="True"
                                 ItemsSource="{Binding}" 
                                 Style="{StaticResource ColumnsFilterListBoxStyle}"
                                 HorizontalAlignment="Left">
                            <ListBox.ItemTemplate>
                                <DataTemplate DataType="x:Type">
                                    <StackPanel Orientation="Horizontal">
                                        <CheckBox  IsChecked="{Binding Path=IsChecked}" Width="20" />
                                        <TextBlock Text="{Binding Path=Name}" Width="Auto" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        <Button Grid.Row="2" Name="BtnColumsChanged" Click="BtnColumsChanged_Click" Width="50">OK</Button>
                    </Grid>
                </Border>
            </Grid>
        </Popup>
        <DataGrid AutoGenerateColumns="False" 
                        Name="gridEvents" 
                        SelectionUnit="Cell"
                        ItemsSource="{Binding Mode=OneWay}"
                        IsReadOnly="True"
                        CanUserSortColumns="False"
                        VirtualizingStackPanel.IsVirtualizing="True"
                        IsSynchronizedWithCurrentItem="True"                                               
                        VerticalContentAlignment="Center" 
                        EnableColumnVirtualization="True"                                            
                        SelectionMode="Extended" 
                        EnableRowVirtualization="True"
                        VirtualizingStackPanel.VirtualizationMode="Recycling"                                                
                        MouseDoubleClick="gridEvents_MouseDoubleClick"                        
                        MouseUp="gridEvents_MouseUp"                        
                        PreviewKeyDown="gridEvents_PreviewKeyDown"
                        LoadingRow="gridEvents_LoadingRow"   
                        SelectedCellsChanged="gridEvents_SelectedCellsChanged"
                        HeadersVisibility="Column"    
                        utils:SearchOperations.SearchTerm="{Binding Path=DataContext.HighlightText, ElementName=usercontrol}"
                        RowDetailsVisibilityMode="Collapsed"  
                        CanUserAddRows="False"                        
                        Style="{StaticResource GridStyle}" CopyingRowClipboardContent="gridEvents_CopyingRowClipboardContent">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Level, Mode=OneWay}" Value="0">
                            <Setter Property="Background"  Value="Black"/>
                            <Setter Property="Foreground" Value="White" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Level, Mode=OneWay}" Value="1">
                            <Setter Property="Background"  Value="Red"/>
                            <Setter Property="Foreground" Value="White" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Level, Mode=OneWay}" Value="2">
                            <Setter Property="Background"  Value="#C23B22"/>
                            <Setter Property="Foreground" Value="White" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Level, Mode=OneWay}" Value="3">
                            <Setter Property="Background" Value="#FFFFFF64" />
                            <Setter Property="Foreground" Value="Black"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Level, Mode=OneWay}" Value="4">
                            <Setter Property="Background" Value="White" />
                            <Setter Property="Foreground" Value="Black"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Level, Mode=OneWay}" Value="5">
                            <Setter Property="Background" Value="#F0F0F0" />
                            <Setter Property="Foreground" Value="Black"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Name="MenuZoomToInterval"  Click="MenuSelectInterval_Click" Cursor="Hand" Header="Select Interval"></MenuItem>
                    <MenuItem Name="MenuChooseColumns"  Header="Choose Columns"  Click="MenuChooseColumns_Click"></MenuItem>
                    <MenuItem Name="MenuShowRowNumber"  Header="Toggle Row numbers"  Click="MenuShowRowNumbers_Click"></MenuItem>
                    <Separator></Separator>
                    <MenuItem Name="MenuCopyWithHeader"  
                              Header="Copy with headers"                                
                              Command="{StaticResource CopyWithHeadersCommand}"
                              CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}"></MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Id"
                                        l:DataGridUtil.Name="Id"
                                        Binding="{Binding Path=Id, Mode=OneWay}"  
                                        CellStyle="{StaticResource IdDataGridCell}"   
                                        Width="50" />
                <DataGridTextColumn Header="Level"
                                        l:DataGridUtil.Name="Level"
                                        Binding="{Binding Path=Level, Mode=OneWay}"  
                                        Width="50" />
                <DataGridTextColumn Header="Keywords"
                                        l:DataGridUtil.Name="Keywords"
                                        Binding="{Binding Path=Keywords, Mode=OneWay}"  
                                        Width="75" />
                <DataGridTextColumn Header="Provider"                                    
                                        l:DataGridUtil.Name="ProviderName"
                                        Binding="{Binding Path=ProviderName, Mode=OneWay}"  
                                        Width="100" />
                <DataGridTextColumn Header="Task"  
                                        l:DataGridUtil.Name="Task"
                                        Binding="{Binding Path=Task, Mode=OneWay}"  
                                        Width="150"/>
                <DataGridTextColumn Header="TaskId"  
                                        l:DataGridUtil.Name="TaskId"
                                        Binding="{Binding Path=TaskId, Mode=OneWay}"  
                                        Width="150"/>
                <DataGridTextColumn Header="Opcode"                                                                           
                                        l:DataGridUtil.Name="Opcode"
                                        Binding="{Binding Path=Opcode, Mode=OneWay}"  
                                        Width="100"  />
                <DataGridTextColumn Header="Symbol"  
                                        l:DataGridUtil.Name="Symbol"
                                        Binding="{Binding Path=Symbol, Mode=OneWay}"   
                                        Width="150" 
                                        CellStyle="{StaticResource SymbolDataGridCell}"/>
                <DataGridTextColumn Header="TimeStamp"
                                        l:DataGridUtil.Name="TimeStamp"
                                        Binding="{Binding Path=TimeStamp, StringFormat={}{0:HH:mm:ss.ffffff}, Mode=OneWay}" 
                                        CellStyle="{StaticResource RightAlign}" 
                                        Width="100" />
                <DataGridTextColumn Header="Delta (ms)"
                                        l:DataGridUtil.Name="TimeFromLastEvent"
                                        Binding="{Binding Path=TimeFromLastEvent, StringFormat=\{0\:N0\} , Mode=OneWay}" 
                                        CellStyle="{StaticResource RightAlign}"                                         
                                        Width="50"
                                        >
                    <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock  FontFamily="Symbol" VerticalAlignment="Stretch">D</TextBlock>
                                <TextBlock VerticalAlignment="Stretch" Padding="3,0,0,0">ms</TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Pid"
                                        l:DataGridUtil.Name="Pid"
                                        Binding="{Binding Path=ProcessId}" 
                                        CellStyle="{StaticResource RightAlign}" 
                                        Width="50" />
                <DataGridTextColumn Header="Tid"  
                                        l:DataGridUtil.Name="Tid"
                                        Binding="{Binding Path=ThreadId}" 
                                        CellStyle="{StaticResource RightAlign}" 
                                        Width="50" />
                <DataGridTextColumn Header="ActivityId"
                                        l:DataGridUtil.Name="ActivityId"                                        
                                        Binding="{Binding Path=ActivityId, Converter={StaticResource GuidToStringConverter}}" 
                                        Width="100" 
                                        CellStyle="{StaticResource ActivityIdCell}"/>
                <DataGridTextColumn Header="RelatedActivityId"  
                                        l:DataGridUtil.Name="RelatedActivityId"                                        
                                        Binding="{Binding Path=RelatedActivityId, Converter={StaticResource GuidToStringConverter}}" 
                                        Width="100" 
                                        CellStyle="{StaticResource RelatedCell}"/>
                <DataGridTextColumn Header="Message"  
                                        l:DataGridUtil.Name="Message"                                        
                                        Binding="{Binding Path=Message}" 
                                        Width="400"                                         
                                        CellStyle="{StaticResource MessageDataGridCell}"/>
                <DataGridTextColumn Header="Context"  
                                        l:DataGridUtil.Name="Context"                                        
                                        Binding="{Binding Path=Context}" 
                                        Width="400"/>
            </DataGrid.Columns>         
            <DataGrid.RowHeaderStyle>
                <Style TargetType="DataGridRowHeader">
                    <Setter Property="Foreground" Value="Black"/>
                </Style>
            </DataGrid.RowHeaderStyle>
        </DataGrid>
    </Grid>
</UserControl>
